package com.me.example;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;



import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.me.experiment.Object;
import com.me.experiment.Scene;
import com.me.experiment.Create;
import com.me.example.data2sd;

import com.me.example.Assets;

import com.me.items.Button;
import com.me.items.Text_box;


/*
Programming example : Numerical discrimination task
2 screens
- presentation of number
- feedback (correct/not correct)

The aim is to do something very similar to a psychtoolbox.
This means dealing with simple algorithmic (for, while, if)
and hiding the object structure to user



*/
public class Example_exp extends Scene{

	
	// Here Variables must be declared
	private SpriteBatch batcher_;

	int i_sujet;
	String sd_root;

	Button b_right;
	Button b_left;

	Text_box tb_present;
	Text_box tb_feedback;
	Text_box tb_touchpoint;
	
	float screen_onset_time;
	float screen_display_time;

	int touched_button;
	int number;
	
	int N_trials_ = 200;

	int numbers[] = new int[N_trials_+1]; 
    float RT[] = new float[N_trials_+1]; // reaction times
	int A[]  = new int[N_trials_+1]; // actions
	float data[][] = new float[3][N_trials_+1];
	
	/**
	 * Initialize the experiment. This is where objects should be specified and the spritebatch created
	 */
	public void init_experiments() {
				

		batcher_ = new SpriteBatch(); // init display
		
		// Setting experiments contingencies
		for(int j=0;j<N_trials_;j++){
			int k = 5;
			while (k==5){
					k = (int)(Math.random()*10);
			}
			numbers[j] = k;
		}
		
		
		Assets.load(); // loading assets for experimets
		Create.init(this); // Initializing item creation
		
		
		// Item creation 
		b_left = Create.newButton(10f, 10f,   300f, 150f, 0);
		b_right = Create.newButton(485f, 10f, 300f, 150f, 0);
		
		tb_present = Create.newText_box(350, 400, "7", 0);
		tb_feedback = Create.newText_box(50, 400, "Correct", 0);
		tb_touchpoint = Create.newText_box(400, 400, "", 0);
				
		graphics_.sortObjects();
		events_.sortObjects();
		
		// Initializing variables for first trial
		i_screen_ = 1;
		i_trial_ = 1;
		
		
		screen_onset_time = getTime();
		screen_display_time = 1;
		
		b_right.show();
		b_left.show();
		tb_present.show();
		tb_feedback.hide();

		tb_touchpoint.str = sd_root;// "100,100";
		tb_touchpoint.hide();
		
		touched_button = 0;
		number = 0;
		
		number = numbers[i_trial_];
		tb_present.setText(Integer.toString(number));
		
		setTime(0f);
		screen_onset_time = getTime();
		screen_display_time = 2;
	}



   @Override
	public void render() {
	   
	   
	if (i_trial_> N_trials_-1){
		try {
			end_of_exp();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}   
	   
	
	events_.processEvents(Gdx.graphics.getDeltaTime()); // this process touch events
	
	

	
	//
	// _______ first screen : digit presentation
	if (i_screen_ == 1){
		
		
		// manage touch event
		if (b_left.touched_){
			b_right.hide(); b_left.touched_ = false; touched_button = 1;
			RT[i_trial_-1] = getTime() - screen_onset_time;
			A[i_trial_-1] = 1;
			}
		if (b_right.touched_){
			b_left.hide(); b_right.touched_ = false;  touched_button = 2;
			RT[i_trial_-1] = getTime() - screen_onset_time;
			A[i_trial_-1] = 2;
			}
		
		// manage transitions
		if ((getTime()> screen_onset_time+screen_display_time)|(b_right.touched_)){
			i_screen_ = 2;
			screen_onset_time = getTime();
			screen_display_time = 2;
			b_right.hide();
			b_left.hide();
			tb_present.hide();
			tb_feedback.show();
			
			
			tb_feedback.setText(" Wrong"); // default

			if ((number<5) & (touched_button == 1)){
				tb_feedback.setText("Correct");
			}

			if ((number>5) & (touched_button == 2)){
				tb_feedback.setText("Correct");
			}
			
			
		}
		
	// _______ second screen : feedback presentation	
	}else if(i_screen_ == 2){
		
		// manage transition
		if (getTime()> screen_onset_time+screen_display_time){
			i_screen_ = 1;
			screen_onset_time = getTime();
			screen_display_time = 2;
			b_right.show();
			b_left.show();
			tb_present.show();
			tb_feedback.hide();
			
			i_trial_ += 1;
			
			number = numbers[i_trial_];
			tb_present.setText(Integer.toString(number));

			
			}
		
	}
	
	
	
	// display
	update(Gdx.graphics.getDeltaTime());  // this updates graphic display
	batcher_.begin();

	Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	present(0f,batcher_);
	batcher_.end();
	
	
		// TODO Auto-generated method stub	
	}

	@Override
	public void init_screen(int i_s) {
		// TODO Auto-generated method stub
	}

	@Override
	public void init_trial(int i_t) {
		// TODO Auto-generated method stub
	}

	
	@Override
	public void create() {
		oncreate();
		init_experiments();
		// TODO Auto-generated method stub
	}
	
	
	public Example_exp(int i) {
		// TODO Auto-generated constructor stub
		i_sujet = i;
	}
	



	public void end_of_exp() throws IOException{

	// filling data
		for (int i = 0 ; i<N_trials_; i++){
			data[0][i] = (float)numbers[i];
			data[1][i] = (float)RT[i];
			data[2][i] = (float)A[i];
		}
		
		String[] head = new String[3]; 
		head[0]=new String ("number"); 
		head[1]=new String ("delay"); 
		head[2]=new String ("answer"); 
		
		data2sd.storedata(head,data);
		dispose();

		

	}

	@Override
	public void dispose() {
		Gdx.app.exit();		
	}



	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	
	

}
